﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using AnOtherBlog.Core.Extensions;

namespace AnOtherBlog.Core.Providers.SQL
{
	/// <summary>
	/// Implementa el acceso a datos para la clase Blog
	/// </summary>
	public partial class SQLDataProvider : DataProvider
	{
		///// <summary>
		///// Obtiene la configuración de un blog de acuerdo su id
		///// </summary>
		///// <param name="id">Identificador del blog</param>
		///// <returns>Blog</returns>
		//public override Blog BlogSelect(int id)
		//{
		//  Blog blog = null;

		//  using (SqlConnection connection = DataConnection)
		//  {
		//    connection.Open();

		//    using (SqlCommand command = new SqlCommand())
		//    {
		//      command.Connection = connection;
		//      command.CommandText = @"SELECT BlogID, BlogDomain, BlogSettings FROM aob_Blogs WHERE BlogID=@BlogID";
		//      command.CommandType = CommandType.Text;
		//      command.Parameters.Add(new SqlParameter("@BlogID", id));
		//      SqlDataReader reader = command.ExecuteReader();
		//      if (reader.Read())
		//      {
		//        if (!reader.IsDBNull(2))
		//        {
		//          blog = new Blog();
		//          blog.BlogID = reader.GetInt32(0);
		//          if (!reader.IsDBNull(1)) blog.BlogDomain = reader.GetString(1);

		//          string data = reader.GetString(2);
		//          blog.BlogSettings = data.ConvertTo<BlogSetting>();
		//        }
		//      }
		//    }
		//  }
		//  return blog;
		//}

		/// <summary>
		/// Obtiene la configuración de un blog de acuerdo a su dominio
		/// </summary>
		/// <param name="domain">Dominio del blog</param>
		/// <returns>Blog</returns>
		public override Blog BlogSelect(string domain)
		{
			Blog blog = null;

			using (SqlConnection connection = DataConnection)
			{
				connection.Open();

				using (SqlCommand command = new SqlCommand())
				{
					command.Connection = connection;
					command.CommandText = @"SELECT BlogID, BlogDomain, BlogSettings FROM aob_Blogs WHERE BlogDomain=@BlogDomain";
					command.CommandType = CommandType.Text;
					command.Parameters.Add(new SqlParameter("@BlogDomain", domain));
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							if (!reader.IsDBNull(2))
							{
								blog = new Blog();
								blog.BlogID = reader.GetInt32(0);
								if (!reader.IsDBNull(1)) blog.BlogDomain = reader.GetString(1);

								string data = reader.GetString(2);
								blog.BlogSettings = data.ConvertTo<BlogSetting>();
							}
						}
					}
				}
			}
			return blog;
		}

		/// <summary>
		/// Inserta la configuración de un blog
		/// </summary>
		/// <param name="blog">Blog a insertar</param>
		public override void BlogInsert(Blog blog)
		{
			using (SqlConnection connection = DataConnection)
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand())
				{
					command.Connection = connection;
					if (blog.BlogID == 0)
					{
						// Esto es una manera de obviar IDENTITY en la tabla de aob_Blogs
						command.CommandText = @"SELECT maxID=max(isnull(BlogID,0)) FROM aob_Blogs ";
						command.CommandType = CommandType.Text;
						command.Parameters.Clear();
						using (SqlDataReader reader = command.ExecuteReader())
						{
							if (reader.Read())
							{
								blog.BlogID = reader.GetInt32(0) + 1;
							}
							else
								throw new Exception("No se puede acceder a la tabla aob_Blogs y obtener el máximo ID");
						}
					}

					command.CommandText = @"INSERT INTO aob_Blogs "
					+ @"( BlogID, BlogDomain, BlogSettings) VALUES "
					+ @"(@BlogID,@BlogDomain,@BlogSettings)";

					command.CommandType = CommandType.Text;
					command.Parameters.Clear();
					command.Parameters.Add(new SqlParameter("@BlogID", blog.BlogID));
					command.Parameters.Add(new SqlParameter("@BlogDomain", blog.BlogDomain));
					command.Parameters.Add(new SqlParameter("@BlogSettings", blog.BlogSettings.ToXml()));
					command.ExecuteNonQuery();
					
				}
			
			}
		}

		/// <summary>
		/// Actualiza la configuración del blog
		/// </summary>
		/// <param name="blog">Blog <see cref="AnOtherBlog.Core.Blog"/></param>
		public override void BlogUpdate(Blog blog)
		{
			using (SqlConnection connection = DataConnection)
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand())
				{
					command.Connection = connection;
					command.CommandText = @"UPDATE aob_Blogs SET BlogDomain = @BlogDomain, BlogSettings = @BlogSettings WHERE BlogID=@BlogID";
					command.CommandType = CommandType.Text;
					command.Parameters.Clear();
					command.Parameters.Add(new SqlParameter("@BlogID", blog.BlogID));
					command.Parameters.Add(new SqlParameter("@BlogDomain", blog.BlogDomain));
					command.Parameters.Add(new SqlParameter("@BlogSettings", blog.BlogSettings.ToXml()));
					command.ExecuteNonQuery();
				}
			}
		}

	}
}
